/*    file: main.cpp
 *    desc:
 * 
 * created: 2016-04-06
 *  author: chuanjiang.zh@qq.com
 * company: 
 */

#include <stdio.h>
#include <iostream>

#include "BasicType.h"
#include "SharedPtr.h"
#include "TFileUtil.h"
#include <assert.h>
#include "TByteBuffer.h"
#include "PsParser.h"

#include "Ffmpeg.h"
extern "C"
{
#include "libavutil/time.h"
}


int main(int argc, char** argv)
{
	if (argc <= 1)
    {
		printf("SplitPs filename");
        return EINVAL;
    }

	av::PsParser parser;

    char* filename = argv[1];
	FILE* file = fopen(filename, "rb");
	if (!file)
	{
		return -1;
	}

	static const int BUFFER_SIZE = 1024;
	uint8_t buffer[1024];

	int videoPacketCount = 0;
	int audioPacketCount = 0;

	while (!feof(file))
	{
		size_t length = fread(buffer, 1, BUFFER_SIZE, file);
		if (length > 0)
		{
			av::PsParser::Packet packet;
			if (parser.parse(buffer, length, packet))
			{
				if (packet.type == av::MEDIA_TYPE_VIDEO)
				{
					videoPacketCount ++;
					comn::FileUtil::write(packet.data, packet.length, "video.data", (videoPacketCount > 1));
				}
				else
				{
					audioPacketCount ++;
					comn::FileUtil::write(packet.data, packet.length, "audio.data", (audioPacketCount > 1));
				}
			}
		}
		else
		{
			break;
		}
	}

	fclose(file);

	return 0;
}




